/**
 * SessionTool.java
 * Copyright © 2015-3015, 杭州泊享网络科技有限公司
 * 
 * @author Liu Tao
 * @create 2013-8-9
 */
package com.pshare.tool;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import com.pshare.constant.SeaxConstants;
import com.pshare.framework.context.SessionContext;
import com.pshare.session.Session;

/**
 * 帐户统一会话工具
 */
public class SessionTool {

	/**
	 * 判断是否登录
	 * 
	 * @return boolean
	 */
	public static boolean isLogin() {
		HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes())
				.getRequest();
		return isLogin(request);
	}

	public static boolean isLogin(HttpServletRequest request) {
		Session session = getSession();
		return session == null ? false : existAttribute(session, SeaxConstants.APPSESSION_ATTR_ACCOUNT);
	}

	public static boolean isLogin(Session session, HttpServletRequest request) {
		return session == null ? false : existAttribute(session, SeaxConstants.APPSESSION_ATTR_ACCOUNT);
	}

	/**
	 * 直接根据sessionId值判断用户是否已登录
	 * 
	 * @param sessionId
	 * @return boolean
	 */
	public static boolean isLogin(String sessionId) {
		if (StringUtils.isBlank(sessionId)) {
			return false;
		}
		Session session = getSession(sessionId);
		return session == null ? false : existAttribute(session, SeaxConstants.APPSESSION_ATTR_ACCOUNT);
	}

	public static boolean isLogin(Session session) {
		HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes())
				.getRequest();
		return isLogin(request);
	}

	public static void logout() {
		HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes())
				.getRequest();
		SessionContext.destroy(request);
	}

	public static void logout(String sessioId) {
		SessionContext.destroy(sessioId);
	}

	/**
	 * 置顶用户销毁session登出
	 * 
	 * @param userId
	 */
	public static void logout(Long userId) {
		SessionContext.destroy(userId);
	}

	public static boolean setAttribute(String key, Object obj) {
		return SessionContext.setAttribute(key, obj);
	}

	public static String setAttributes(Map<String, Object> map) {
		return SessionContext.setAttributes(map);
	}

	public static Object getAttribute(String key) {
		if (StringUtils.isBlank(key)) {
			return null;
		}
		return getSession().getAttribute(key);
	}

	/**
	 * 刷新指定帐户的会话缓存，一般在修改帐户信息后应该执行该操作
	 * 
	 * @param user
	 */
	public static void refreshSession(Object user) {
		Session session = getSession();
		session.setAttribute(SeaxConstants.APPSESSION_ATTR_ACCOUNT, user);
		SessionContext.fresh();
	}

	public static Session getSession() {
		return SessionContext.getSession();
	}

	public static Session getSession(HttpServletRequest request) {
		return SessionContext.getSession(request);
	}

	public static Session getSession(String sessionId) {
		return SessionContext.getSession(sessionId);
	}

	/**
	 * 读取session，如果没有，就返回空值
	 * 
	 * @param sessionId
	 * @return Session
	 */
	public static Session readSession(String sessionId) {
		return SessionContext.readSession(sessionId);
	}

	public static Session readSession() {
		return SessionContext.readSession();
	}

	public static void flushSession() {
		Session session = getSession();
		session.destroy();
	}

	/**
	 * 清理所有缓存数据。慎用
	 */
	public static void flushAll() {
		SessionContext.flush();
	}

	/**
	 * 读取缓存中的客户端类型
	 * 
	 * @return Intger 1:用户端，2：机构端
	 */
	public static Integer getIdClientType() {
		Session session = getSession();
		return session == null ? null : (Integer) session.getAttribute(SeaxConstants.APPSESSION_ATTR_IDCLIENTTYPE);
	}

	/**
	 * 读取缓存中的用户ID
	 * 
	 * @return Long
	 */
	public static Long getUserId() {
		//return 6039238564712364033L;
		//return 6037818526117738497L;
		//return 6024826099715094529L;
		//return 6019351494791608321L;
		Session session = getSession();
		return session == null ? null : (Long) session.getAttribute(SeaxConstants.APPSESSION_ATTR_ACCOUNT_ID);
	}

	/**
	 * 读取缓存中的用户设备信息ID
	 * 
	 * @return Long
	 */
	public static String getDeviceToken() {
		Session session = getSession();
		return session == null ? null : (String) session.getAttribute(SeaxConstants.APPSESSION_ATTR_DEVICETOKEN);
	}

	/**
	 * @return
	 */
	public static Integer getOsType() {
		Session session = getSession();
		return session == null ? null : (Integer) session.getAttribute(SeaxConstants.APPSESSION_ATTR_OSTYPE);
	}

	private static boolean existAttribute(Session session, String key) {
		Object obj = session.getAttribute(key);
		return obj != null;
	}

	/**
	 * @param userId
	 * @return
	 */
	public static Session getSessionByUserId(Long userId) {
		return SessionContext.getSessionByUserId(userId);
	}

}
